package com.zhengtoudai.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.BaseActivity;
import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.component.DaggerActivityComponent;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.ui.adapter.InvestmentRecordAdapter;
import com.zhengtoudai.ui.bean.Result;
import com.zhengtoudai.ui.bean.TenderRecord;
import com.zhengtoudai.ui.presenter.TenderRecordPresenterImpl;
import com.zhengtoudai.ui.view.CommonView;
import com.zhengtoudai.util.DensityUtil;
import com.zhengtoudai.view.swipetoloadlayout.base.OnRefreshListener;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;


//投资记录
public class InvestmentRecordActivity extends BaseActivity implements OnRefreshListener,CommonView<TenderRecord> {

    @Bind(R.id.swipe_target)
    RecyclerView swipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout refresh;

    @Inject
    TenderRecordPresenterImpl mRecordPresenter;
    private InvestmentRecordAdapter adapter;

    private String id,type;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_invest_ment_record);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        id=getIntent().getStringExtra("id");
        type=getIntent().getStringExtra("type");
    }

    @Override
    protected void initTitle() {
        if (type.equals("borrowId")){
            titleUtil.setTitle("投资记录");
        }else if (type.equals("transId")){
            titleUtil.setTitle("认购记录");
        }
    }

    @Override
    protected void initView() {
        DaggerActivityComponent.builder()
                .appComponent(App.mApp.getAppComponent())
                .activityModule(new ActivityModule(this))
                .build()
                .inject(this);
        mRecordPresenter.attachView(this);

        refresh.setOnRefreshListener(this);
        refresh.setLoadMoreEnabled(false);
        swipeTarget.setLayoutManager(new LinearLayoutManager(context));
        adapter=new InvestmentRecordAdapter(type);
        swipeTarget.setAdapter(adapter);
        swipeTarget.setPadding(0, DensityUtil.dip2px(context,10),0,0);
        status.showLoading();
        refresh.post(new Runnable() {
            @Override
            public void run() {
                refresh.setRefreshing(true);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRecordPresenter.detachView();
    }

    private void loadData(){
        if (type.equals("borrowId")){
            mRecordPresenter.tenderRecord(id);
        }else if (type.equals("transId")){
            mRecordPresenter.offerToBuyRecord(id);
        }
    }

    @Override
    public void onRefresh() {
        loadData();
    }

    @Override
    public void showDialog() {
        if (type.equals("borrowId")){
           showProgressDialog("正在获取投标记录...");
        }else{
            showProgressDialog("正在获取认购记录...");
        }
    }

    @Override
    public void onResult(Result<TenderRecord> result) {
        status.removeView();
        if (result.getCode()== Config.SUCCESSFUL_CODE){
            if (result.getResult().getDataList()!=null&&result.getResult().getDataList().size()>0){
                adapter.clearData();
                adapter.addData(result.getResult().getDataList());
            }else{
                status.showNoData("暂无记录");
                //AbToastUtil.showToast(context,"暂无记录");
            }
        }else{
            status.showNoData(result.getMessage());
            //AbToastUtil.showToast(context,result.getMessage());
        }
    }

    @Override
    public void onFailure(Throwable e) {
        status.showNoData(HttpUtil.getFailure(context, e), "重新加载", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                status.showLoading();
                if (type.equals("borrowId")){
                    mRecordPresenter.tenderRecord(id);
                }else if (type.equals("transId")){
                    mRecordPresenter.offerToBuyRecord(id);
                }
            }
        });
        //HttpUtil.disposeOnFailure(context,e);
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
        onComplete(refresh);
    }
}
